# 管道机制
import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(-3, 3, size=100)
X = x.reshape(-1, 1)
y0 = 0.5 * x ** 2 + x + 2
y = y0 + np.random.normal(0, 1, size=100)

from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import StandardScaler

degree = 3
# 这里将三个处理步骤进行了封装，将数据传入poly_reg之后，将会智能地沿着该管道进行处理
poly_reg = Pipeline([
    ("poly", PolynomialFeatures(degree=degree)),
    ("std_scaler", StandardScaler()),
    ("lin_reg", LinearRegression())
])

poly_reg.fit(X, y)

y_predict = poly_reg.predict(X)

plt.scatter(x, y)
plt.plot(np.sort(x), y_predict[np.argsort(x)], color='r')
plt.plot(np.sort(x), y0[np.argsort(x)], color='g')
plt.show()

